﻿package kliment.data {
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	
	/**
     * Dispatched when open request
     */
    [Event(name = 'open', type = 'flash.events.Event')]
	/**
     * Dispatched when complete request
     */
    [Event(name = 'complete', type = 'flash.events.Event')]
	
	/**
	 * ...
	 * @author	Kliment
	 * @version	1.2.5
	 * 
	 * class name: kliment.data.Form
	 */
	public class Form extends EventDispatcher{
		private var _fields:Array;
		private var _request_url:String;
		private var _method:String;
		private var _answer:String;
		private var _urlLoader:URLLoader;
		
		public function Form(fields:Object, request_url:String, method:String = URLRequestMethod.POST) {
			_fields = new Array();
			_request_url = request_url;
			_method = method;
			
			for (var i:* in fields) 
				addField(fields[i] as FormField);
		}
		
		public function submit():Boolean {
			_answer = null;
			var data:URLVariables = new URLVariables();
			var length:Number = _fields.length
			for (var i:Number = 0; i < length; i++) {
				var fieldName:FormField = _fields[i];
				if (!fieldName.isValid)
					return false;
				data[fieldName.name] = fieldName.value;
			}
			
			var urlReqesr:URLRequest = new URLRequest();
			urlReqesr.url = _request_url; 
			urlReqesr.method = _method; 
			urlReqesr.data = data;
			
			if (_urlLoader) {
				_urlLoader.removeEventListener(Event.OPEN, _urlLoaderHandler);
				_urlLoader.removeEventListener(Event.COMPLETE, _urlLoaderHandler);
				_urlLoader.close();
			}
			_urlLoader = new URLLoader();
			_urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
			_urlLoader.addEventListener(Event.OPEN, _urlLoaderHandler);
			_urlLoader.addEventListener(Event.COMPLETE, _urlLoaderHandler);
			_urlLoader.load(urlReqesr);
			
			return true;
		}
		
		private function _urlLoaderHandler(event:Event):void {
			switch(event.type) {
				case Event.OPEN:
					dispatchEvent(new Event(Event.OPEN))
				break;
				
				case Event.COMPLETE:
					_answer = _urlLoader.data;
					_urlLoader.removeEventListener(Event.OPEN, _urlLoaderHandler);
					_urlLoader.removeEventListener(Event.COMPLETE, _urlLoaderHandler);
					dispatchEvent(new Event(Event.COMPLETE))
				break;
			}
		}
		
		public function addField(value:FormField):void {
			_fields.push(value);
			value.form = this;
		}
		
		public function clear():void {
			var i:Number = _fields.length;
			while(i--){
				var fieldName:FormField = _fields[i] as FormField;
				fieldName.clear();
			}
		}
		
		public function remove():void {
			while (_fields.length) {
				var item:FormField = _fields.shift() as FormField;
				item.remove();
				item = null;
			}
		}
		
		public function get answer():String { return _answer; }
	}

}